package chapter04.sync;

import lombok.extern.slf4j.Slf4j;

// 两个线程必须先打印2再打印1
@Slf4j
public class TwoOneTest1 {
    static boolean flag=false;
    public static void main(String[] args) throws InterruptedException {

        Thread t1 = new Thread(() -> {
            synchronized (TwoOneTest1.class){
                if (!flag){
                    try {
                        TwoOneTest1.class.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                log.debug("{}",1);
            }
        }, "t1");

        Thread t2 = new Thread(() -> {
            synchronized (TwoOneTest1.class){
                log.debug("{}",2);
                flag=true;
                TwoOneTest1.class.notifyAll();
            }
        }, "t2");
        t1.start();
        Thread.sleep(1000);
        t2.start();

    }
}
